﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrainingsApp.Logic.Import.Template;
using TrainingsApp.Logic.Import.Model;
using TrainingsApp.Model;

namespace TrainingsApp.Logic.Import {
    public class ChapterTypeImport : ImportBase {

        ITemplateReader m_reader = new Txt_ChapterDataReader_1();

        public override bool Import(int metalsId, string location, TrainingsApp.Model.ImportType type) {
            if (type == ImportType.Chapter && (m_reader is XLS_ChapterDataReader_1)) {
                m_reader = new Txt_ChapterDataReader_1();
            }
            else {
                if (!(m_reader is XLS_ChapterDataReader_1))
                    m_reader = new XLS_ChapterDataReader_1();
            }
            Dictionary<int, object> dict = m_reader.ReadAll(metalsId, location, 0/*ALL*/);
            foreach (var item in dict.Values) {
                ChapterTemplate_1 itemList = item as ChapterTemplate_1;
                //var insertEntity = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Add(new TS_TeachMaterials_Chapter_Entity {
                //    IsLeaf = false,
                //    MaterialsId = 1,
                //    ParentIdx = 0,
                //    Name = itemList.Name,
                //    RootPath = string.Empty
                //});
                Import(itemList, 0, itemList.MetalsId);
            }
            return true;
        }

        private void Import(ChapterTemplate_1 entity, long parentId, long MaterialsId) {

            var insertEntity = TS_TeachMaterials_Chapter_Logic.TS_TeachMaterials_Chapter_Add(new TS_TeachMaterials_Chapter_Entity {
                IsLeaf = entity.Children.Count == 0,
                MaterialsId = MaterialsId,
                ParentIdx = parentId,
                Name = entity.Name,
                RootPath = string.Empty,
                Code = entity.Code,
                IsDelete = 0
            });

            foreach (var item in entity.Children) {
                Import(item, insertEntity.Idx.Value, MaterialsId);
            }
        }
    }
}
